<template>
  <div class="box">
    <van-nav-bar title="修改密码"  left-arrow @click-left="goBack"></van-nav-bar>
    <van-form class="main">
      <van-field
        v-model="username"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, pattern: pattern1, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="新密码"
        label="新密码"
        placeholder="请输入新密码"
        :rules="[{ required: true, pattern: pattern2, message: '请填写新密码' }]"
      />
      <van-field
        v-model="sms"
        center
        clearable
        label="短信验证码"
        placeholder="请输入短信验证码"
      >
        <template #button>
          <van-button size="small" type="primary" @click="sendCode">发送验证码</van-button>
        </template>
      </van-field>
      <div style="margin-top: 0.16rem;">
        <van-button round block type="info" native-type="submit" @click="Verification">
          提交
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import Vue from 'vue'
import { Icon, NavBar, Field, Toast, Form, Button } from 'vant'
import { updataPassword, sendCode } from '@/api'
Vue.use(NavBar)
Vue.use(Icon)
Vue.use(Field)
Vue.use(Toast)
Vue.use(Button)
Vue.use(Form)
export default {
  data () {
    return {
      username: '',
      password: '',
      sms: '',
      pattern1: /^1[3456789]\d{9}$/,
      pattern2: /^([a-zA-Z0-9]+[.~!@#$%-^&()]*){6,}/
    }
  },
  mounted () {},
  methods: {
    goBack () {
      this.$router.go(-1)
    },
    Verification () {
      updataPassword({
        tel: this.username,
        password: this.password,
        usercode: this.sms
      }).then(res => {
        // console.log(res)
        if (res.data.code === '200') {
          Toast('密码修改成功,即将跳转登录页')
          setTimeout(() => {
            this.$router.replace('/login')
          }, 500)
        }
      })
    },
    sendCode () {
      sendCode({
        tel: this.username
      }).then(res => {
        // console.log(res)
        Toast('发送成功,请5分钟内验证')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.main {
  width: 100%;
  height: auto;
  padding: 0 0.15rem;
}
/deep/ .van-nav-bar {
  border-bottom: 1px solid #ebebeb;
}
/deep/ .van-icon-arrow-left {
  font-size: 0.2rem;
  color: black;
}
/deep/ .van-field {
  width: 100%;
  height: 0.6rem;
  padding: 0;
  color: #232323;
  font-size: 0.16rem;
  line-height: 0.6rem;
  border-bottom: 1px solid #EEEEEE;
}
</style>
